import Vue from 'vue'
// import Vue from 'vue'
import router from "../router"
export default {
    namespaced: true,
    state() {
        return {
            userInfo: null,
            cartList: [
                {
                    id: 1,
                    "shopname": "私家古茶店",
                    "is_checked": true,
                    product: "土林凤凰云南无量山普洱茶砖250克熟茶糯米香普洱茶砖茶叶607特级",
                    "product_img": "https://chadian-img.oss-cn-shanghai.aliyuncs.com/Upload/bb8a7c4703314fd9940fb852f46d2058.jpg",
                    "shop_price": "80.00",
                    num: 2.
                },
                {
                    id: 2,
                    "shopname": "兴隆轩茶业旗舰店 多年老店",
                    "is_checked": true,
                    product: "2021年新茶秋茶高山兰花香安溪铁观音茶叶乌龙茶浓香型铁观音500g",
                    "product_img": "https://chadian-img.oss-cn-shanghai.aliyuncs.com/product/i16026879570000660.jpeg?x-oss-process=image/resize,m_fill,w_120,h_120",
                    "shop_price": "569",
                    num: 2.
                },
                {
                    id: 3,
                    "shopname": "云南普洱老茶店",
                    "is_checked": true,
                    product: "云南临沧 勐库戎氏2020年木叶醇香 普洱熟茶饼 357克",
                    "product_img": "https://chadian-img.oss-cn-shanghai.aliyuncs.com/product/a15951517263838959.jpeg?x-oss-process=image/resize,m_fill,w_120,h_120",
                    "shop_price": "288.00",
                    num: 3.
                }
            ]
        }
    },
    getters: {
        allInfo(state) {
            let allCheck = true
            let checkPrice = 0
            let checkNum = 0
            let allNum = 0
            let checkList=[]

            state.cartList.forEach(val => {
                allNum += val.num
                if (!val.is_checked) {
                    allCheck = false
                }
                if (val.is_checked) {
                    checkPrice += val.num * val.shop_price
                    checkNum += val.num
                    checkList.push(val)
                }
            })
            let allChecked = allCheck
            return {
                allCheck, allChecked, checkPrice, checkNum, allNum,checkList
            }
        }
    },
    mutations: {
        checkMut(state, idx) {
            state.cartList[idx].is_checked = !state.cartList[idx].is_checked
        },
        AllCheckMut(state, bool) {
            state.cartList.forEach(item => {
                item.is_checked = !bool
            })
        },
        addNumMut(state, { idx, bool }) {
            if (bool) {
                state.cartList[idx].num++
            } else {
                state.cartList[idx].num--
            }
        },
        DelCheckMut(state) {
            for (let i = 0; i < state.cartList.length; i++) {
                if (state.cartList[i].is_checked == true) {
                    state.cartList.splice(i, 1)
                    i--
                }
            }
        },
        DelOneMut(state, idx) {
            state.cartList.splice(idx, 1)
        },
        initUserInfoMut(state, info) { //此处的info中会包含token
            state.userInfo = info
            if (info) {
                localStorage.setItem('userInfo', JSON.stringify(info))
            } else {
                localStorage.removeItem('userInfo')
                router.push('/')
            }
        },
        addCartMut(state,Info){ //obj是新商品对象
            let {cartList} = state
            let len = cartList.length
            console.log(state.cartList);
            console.log(Info);
            for(let i=0;i<len;i++){
                if(cartList[i].id === Info.id){ //购物车已有相同商品
                    state.cartList[i].num++
                    return
                }
            }
            Info.is_checked=true,
            // Info.num=1 
            Vue.set(Info,"num",1) 
            state.cartList.push(Info) 
          },
    }
}